package com.atguigu.queen;

import java.util.Scanner;

public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArrayQueue Queue = new CircleArrayQueue(6);
        char   key;
        Scanner scanner = new Scanner(System.in);
        boolean   loop=true;
        while (loop){
            System.out.println("a(add):添加数据");
            System.out.println("g(get):获取数据，出队列");
            System.out.println("s(show):显示数据");
            System.out.println("h(head):显示头队列");
            System.out.println("e()exit:退出程序");
            key=scanner.next().charAt(0);
            switch (key){
                case   's':
                    Queue.showQueue();
                    break;
                case 'a':
                    System.out.println("输入一个数");
                    int value = scanner.nextInt();
                    Queue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int  a=Queue.getQUeue();
                        System.out.printf("取出的数据是%d\n",a);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int  s=Queue.headQueue();
                        System.out.printf("取出的对头为%d\n",s);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
                default:
                    break;

            }

        }
        System.out.println("程序退出");

    }
}
class   CircleArrayQueue{
    private  int  maxsize;
    private   int  rear;
    private  int  front;
    private   int   [] arr;
    public  CircleArrayQueue(int  ArrMaxsize){
        maxsize=ArrMaxsize;
        arr=new int[maxsize];
        rear=0;
        front=0;
    }
    //判断队列是否满了
    public   boolean   isFull(){
        return   (rear+1)%maxsize==front;
    }
    //判断队列是否为空
    public   boolean   isEmpty(){
        return   rear==front;
    }
    //添加数据到队列
    public void   addQueue(int  n){
        if (isFull()){
            System.out.println("队列已经满了");
        }else {
            arr[rear]=n;
            rear=(rear+1)%maxsize;
        }
    }
    //获取队列的数据，出队列
    public  int  getQUeue(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空，无法进行出队列操作");
        }
        int value = arr[front];
        front=(front+1)%maxsize;
        return  value;
    }
    //显示队列
    public   void   showQueue(){
        if (isEmpty()){
            System.out.println("队列为空，无法进行去数据");
        }
        for (int  i=front;i<front+size();i++){
            System.out.printf("arr[%d]=%d\n",i%maxsize,arr[i%maxsize]);
        }
    }
    public   int  size(){
        return   (maxsize-front+rear)%maxsize;
    }
    //显示队列的队头
    public  int    headQueue(){
        if (isEmpty()){
            throw  new   RuntimeException("队列为空，没有数据");
        }
        return   arr[front];
    }
}

